////合并上下字段(colIdx)
jQuery.fn.rowspan = function(colIdx) {
    return this.each(function() {
        var that;
        $('tr', this).each(function(row) {
            var thisRow = $('td:eq(' + colIdx + '),th:eq(' + colIdx + ')', this);
            if ((that != null) && ($(thisRow).html() == $(that).html())) {
                rowspan = $(that).attr("rowSpan");
                if (rowspan == undefined) {
                    $(that).attr("rowSpan", 1);
                    rowspan = $(that).attr("rowSpan");
                }
                rowspan = Number(rowspan) + 1;
                $(that).attr("rowSpan", rowspan);             
                $(thisRow).remove(); ////$(thisRow).hide();
            } else {
                that = thisRow;
            }
            that = (that == null) ? thisRow : that; 
        });
        alert('1');
    });
}

////当指定字段(colDepend)值相同时，才合并域(colIdx)
jQuery.fn.rowspan = function(colIdx, colDepend) {
    return this.each(function() {
        var that;
        var depend;
        $('tr', this).each(function(row) {
            var thisRow = $('td:eq(' + colIdx + '),th:eq(' + colIdx + ')', this);
            var dependCol = $('td:eq(' + colDepend + '),th:eq(' + colDepend + ')', this);
            if ((that != null) && (depend != null) && ($(thisRow).html() == $(that).html()) && ($(depend).html() == $(dependCol).html())) {
                rowspan = $(that).attr("rowSpan");
                if (rowspan == undefined) {
                    $(that).attr("rowSpan", 1);
                    rowspan = $(that).attr("rowSpan");
                }
                rowspan = Number(rowspan) + 1;
                $(that).attr("rowSpan", rowspan);
                $(thisRow).remove(); ////$(thisRow).hide();
                
            } else {
                that = thisRow;
                depend = dependCol;
            }
            that = (that == null) ? thisRow : that;
            depend = (depend == null) ? dependCol : depend;
        });
    });
}

////合并左右字段
jQuery.fn.colspan = function(rowIdx) {
    return this.each(function() {
        var that;
        $('tr', this).filter(":eq(" + rowIdx + ")").each(function(row) {
            $(this).find('th,td').each(function(col) {
                if ((that != null) && ($(this).html() == $(that).html())) {
                    colspan = $(that).attr("colSpan");
                    if (colspan == undefined) {
                        $(that).attr("colSpan", 1);
                        colspan = $(that).attr("colSpan");
                    }
                    colspan = Number(colspan) + 1;
                    $(that).attr("colSpan", colspan);
                    $(this).remove(); 
                } else {
                    that = this;
                }
                that = (that == null) ? this : that; 
            });
        });
    });
}

//控件ID特殊字符转义
function ReplaceID(id){
 var ret = id.replace(/:/g,"\\:");
     ret = ret.replace(/\./g,"\\.");
     ret = ret.replace(/\//g,"\\/");
     ret = ret.replace(/\$/g,"\\$");
     ret = ret.replace(/\[/g,"\\[");
     ret = ret.replace(/\]/g,"\\]");
     return ret;
}
//增加身份证验证
function isIdCardNo(num) {
    var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
    var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
    var varArray = new Array();
    var intValue;
    var lngProduct = 0;
    var intCheckDigit;
    var intStrLen = num.length;
    var idNumber = num;
    // initialize
    if ((intStrLen != 15) && (intStrLen != 18)) {
        return false;
    }
    // check and set value
    for (i = 0; i < intStrLen; i++) {
        varArray[i] = idNumber.charAt(i);
        if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
            return false;
        } else if (i < 17) {
            varArray[i] = varArray[i] * factorArr[i];
        }
    }
    if (intStrLen == 18) {
        //check date
        var date8 = idNumber.substring(6, 14);
        if (isDate8(date8) == false) {
            return false;
        }
        // calculate the sum of the products
        for (i = 0; i < 17; i++) {
            lngProduct = lngProduct + varArray[i];
        }
        // calculate the check digit
        intCheckDigit = parityBit[lngProduct % 11];
        // check last digit
        if (varArray[17] != intCheckDigit) {
            return false;
        }
    }
    else {        //length is 15
        //check date
        var date6 = idNumber.substring(6, 12);
        if (isDate6(date6) == false) {
            return false;
        }
    }
    return true;
}
function isNull(str){
	if (str==null || $.trim(str)=='')
	{
		return true;
	}
	return false;
}
function isNotNull(str){
	if (str!=null && $.trim(str)!='')
	{
		return true;
	}
	return false;
}
function isEmail(str){
	return true;
}
function isMobile(str){
	 var reg = new RegExp("^1[0-9]{10}$");
	return reg.test(str);
}
function isInt(str){
	 var reg = new RegExp("^-?[0-9]+$");
	return reg.test(str);
}
	function gotoPage(n){
	
		if(isInt(n)){
			var f = document.searchForm;
			f.pageNo.value=n;
			//f.totalCount.value = -1;
			f.submit();
		}
		
	}

	function doSubmit(){
		var f = document.searchForm;
		f.totalCount.value = -1;
		f.pageNo.value=1;
		f.submit();
	}
	
	function doSubmitNoPaging(){
		var f = document.searchForm;
		f.submit();
	}
	
	function query(){
		var f = document.searchForm;
		//f.totalCount.value = -1;
		f.submit();
	}
function doReset(f)
{
   //var f = this.document.searchForm;
   var c = f.elements;
   for (var i = 0; i < c.length; i ++ )
   {
      var o = f.elements[i];
      if (o.tagName == "SELECT")
      o.selectedIndex = 0;
      else
      if (o.type && o.type == "text")
      {
         if (o.name != "pageSize")
         o.value = "";

      }else if (o.type && o.type == "hidden")
      {
		o.value = "0";
      }else if (o.tagName == "TEXTAREA")
      {
    		o.value = "";
      }else if (o.type && o.type == "file")
      {
  		o.value = "";
      }

   }
}
	function batchCheckMe(obj,objName){
		var checked = obj.checked;
		var ids = document.getElementsByName(objName);
		for (var j=0; j<ids.length; j++)
		{
			ids[j].checked = checked;
		}
	}

// checkbox全选
function checkall(){
	var checkallBtn = $(".checkAll");
	var checkBtn = $("input[type='checkbox']");
	var cname;

	checkallBtn.children("input[type='checkbox']").click(function(){
		cname = $(this).parent(".checkAll").attr("data-checkname");
		$("input[name='"+cname+"']").prop("checked", this.checked);
	});
	checkallBtn.click(function(){
		var cname = $(this).attr("data-checkname");
		var pc = $(this).children("input[type='checkbox']");
		$("input[name='"+cname+"']").prop("checked",!pc.prop("checked"));
	});


	checkBtn.click(function(){
		cname = $(this).attr("name");
		var $subs = $("input[name='"+cname+"']");
		$(".checkAll[data-checkname='"+cname+"']").children("input[type='checkbox']").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
	});
	$(".checkvalue").click(function(){
		cname = $(this).attr("name");
		var $subs = $("input[name='"+cname+"']");
		$(".checkAll[data-checkname='"+cname+"']").children("input[type='checkbox']").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
	});
}

// check范围&全选
function checkvalue(){
	var checkBtnV = $(".checkvalue");
	var checkBtn = $(".checkvalue input[type='checkbox']");
	var checkallBtnV = $(".checkAll");
	var checkallBtn = $(".checkAll input[type='checkbox']");

	checkBtnV.click(function(ev){
		ev.stopPropagation();
		var pc = $(this).children("input[type='checkbox']");
		pc.prop("checked", !pc.prop("checked"));
		var cname = pc.attr("name");
		var $subs = $("input[name='"+cname+"']");
		$(".checkAll[data-checkname='"+cname+"']").children("input[type='checkbox']").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
	})

	checkBtn.click(function(ev){
		ev.stopPropagation();
		var cname = $(this).attr("name");
		var $subs = $("input[name='"+cname+"']");
		$(".checkAll[data-checkname='"+cname+"']").children("input[type='checkbox']").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
	})

	checkallBtnV.click(function(ev){
		ev.stopPropagation();
		var cname = $(this).attr("data-checkname");
		var pc = $(this).children("input[type='checkbox']");
		$("input[name='"+cname+"']").prop("checked",pc.prop("checked"));
	});

	checkallBtn.click(function(ev){
		ev.stopPropagation();
		cname = $(this).parent(".checkAll").attr("data-checkname");
		$("input[name='"+cname+"']").prop("checked", this.checked);
	});
}



// 表单选择日期
function formdate(){
	$(".datepicker .date").datepicker({
	    "format": "yyyy-mm-dd",
	    "autoclose": true
	});

	$(".datepicker").datepair();
}

			// leftnav
function leftnav(){	
	$('.lnav2ul li.on').parents('.leftnavli').addClass('on').addClass('active');

	$('.leftnavli h2 a').click(function(){
		$('.leftnavli').removeClass('active');
		$(this).parents('.leftnavli').addClass('active');
	})
}
function selectCitys(){
$(".c_Regins").change(function(){//alert(getProvidersUrl);
		var div = $(this).parent().parent();
		var countrys = div.find("select").eq(1);	
		$.getJSON(getRegionsUrl + $(this).val()+"&ram="+Math.random(),
				function(data) {
					countrys.empty();
					var option = "<option value='0'>请选择</option>";
					countrys.append(option);
//					if(data.length < 1) {
//						alert("该区域无国家，请先维护国家信息。");
//					}
					$.each(data, function(i,item){
						option = "<option value='"+data[i].id+"'>"+data[i].countryName+"</option>";
						countrys.append(option);
					  });
				});
	});
	$(".c_countrys").change(function(){
		var div = $(this).parent().parent();
		//var providers = div.find("select").eq(1);
		var citys = div.find("select").eq(2);
		$.getJSON(getCitysUrl + $(this).val()+"&ram="+Math.random(),
				function(data) {
					citys.empty();
					var option = "<option value='0'>请选择</option>";
					citys.append(option);
					//if(data.length < 1) {
					//	alert("该区域无国家，请先维护国家信息。");
					//}
					$.each(data, function(i,item){
						option = "<option value='"+data[i].id+"'>"+data[i].cityName+"</option>";
						citys.append(option);
					  });
				});
	});
}

function selectGoods(){
$(".c_productTypes").change(function(){//alert(getProvidersUrl);
		var div = $(this).parent().parent();
		var providers = div.find("select").eq(1);	
		$.getJSON(getProvidersUrl + $(this).val()+"&ram="+Math.random(),
				function(data) {
					providers.empty();
					var option = "<option value='0'>请选择</option>";
					providers.append(option);
//					if(data.length < 1) {
//						alert("该区域无国家，请先维护国家信息。");
//					}
					$.each(data, function(i,item){
						option = "<option value='"+data[i].id+"'>"+data[i].providerName+"</option>";
						providers.append(option);
					  });
				});
	});
	$(".c_providers").change(function(){
		var div = $(this).parent().parent();
		var providers = div.find("select").eq(1);
		var productTypes = div.find("select").eq(0);
		var products = div.find("select").eq(2);
		$.getJSON(getProductsUrl+productTypes.val()+"&providerId=" + $(this).val()+"&ram="+Math.random(),
				function(data) {
					products.empty();
					var option = "<option value=''>请选择</option>";
					products.append(option);
					//if(data.length < 1) {
					//	alert("该区域无国家，请先维护国家信息。");
					//}
					$.each(data, function(i,item){
						option = "<option value='"+data[i].id+"'>"+data[i].productName+"</option>";
						products.append(option);
					  });
				});
	});
	$(".c_products").change(function(){
		var div = $(this).parent().parent();
		var goods = div.find("select").eq(3);//alert(1)
		$.getJSON(getGoodsUrl + $(this).val()+"&math="+Math.random(),
				function(data) {//alert(1);
					goods.empty();
					var option = "<option value='0'>请选择</option>";
					goods.append(option);
					//if(data.length < 1) {
					//	alert("该区域无国家，请先维护国家信息。");
					//}
					$.each(data, function(i,item){
						option = "<option value='"+data[i].id+"'>"+data[i].goodName+"</option>";
						goods.append(option);
					  });
				});
	});	
}

// addbtn
function addbtn(){
	// 不可用日期添加-删除
	var addbtn = $(".addbtn");
	var closebtn = $(".closebtn");

	addbtn.each(function(){
		var addcon = $(this).prev(".addtext");
		var addtext = addcon.html();
		//addcon.after("<div class='addtext'>"+addtext+"</div>");
		//formdate();
		//addcon.hide();
		$(this).click(function(){
			$(this).after("<div class='addtext'>"+addtext+"</div>");
			formdate();
			selectCitys();
			selectGoods();
			var i = addcon.parent().children(".addtext").length - 2;//alert(i);
			addcon.parent().children(".addtext").eq(i).append("<div class='closebtn'></div>");
			$(".closebtn").click(function(){
				$(this).parent().remove();
			})
		})	
	})
	closebtn.each(function(){
			$(this).click(function(){
				$(this).parent().remove();
			})			
			});
}

//加载中
function showMask(){
		var htm = '<div class="m_mask"></div>';
			htm += '<div class="m_loading"><div class="loading_img">';
			htm += "<img src='../../../images/loading-line.gif' /></div>";
			htm += "<div class='loading_font'>加载中，请稍候...</div></div>";		
		$('.main_wrap').append(htm);
		$(".m_mask").css("display","block");
		$(".m_loading").css("display","block");
	}

//加载中
function showMask2(){
		var htm = '<div class="m_mask"></div>';
			htm += '<div class="m_loading"><div class="loading_img">';
			htm += "<img src='../images/loading-line.gif' /></div>";
			htm += "<div class='loading_font'>加载中，请稍候...</div></div>";		
		$('.main_wrap').append(htm);
		$(".m_mask").css("display","block");
		$(".m_loading").css("display","block");
	}

//加载中
function showMask3(imgPath,loading){
		var htm = '<div class="m_mask"></div>';
			htm += '<div class="m_loading"><div class="loading_img">';
			htm += "<img src='"+imgPath+"' /></div>";
			htm += "<div class='loading_font'>"+loading+"</div></div>";		
		$('.main_wrap').append(htm);
		$(".m_mask").css("display","block");
		$(".m_loading").css("display","block");
	}

 function hideMask(){
	  $(".m_mask").css("display","none");
	  $(".m_loading").css("display","none");
	}